﻿<%@ WebHandler Language="C#" Class="RTGS.Web.ajaxhandler.usermanagement.sys_t_log" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using RTGS.Library;
using RTGS.Library.DataAccess.UserManagement;
using RTGS.Library.Entities.UserManagement;
using RTGS.Library.Enum;
using RTGS.Web.support;
namespace RTGS.Web.ajaxhandler.usermanagement
{

public class sys_t_log : RTGS.Web.BaseHandler
    {

        private  sysTLogRepo logDal = new sysTLogRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        private string MethodName = string.Empty;

       
        private sysTLog log = new sysTLog();

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
    
            MethodName = context.Request.Params["method"];

            context.Response.Clear();
            context.Response.Buffer = true;

            switch (MethodName.ToLower())
            {
                
                case ("delete"):
                    context.Response.Write(Delete(context));
                    break;
                case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;

                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region CRUD Handler


        private string Delete(HttpContext context)
        {
            JSONHelper helper = new JSONHelper();

            bool isSuccess = true;
            string[] ids = context.Request.Params["id"].Split(',');
            foreach (string id in ids)
            {
                int intID = Convert.ToInt32(id);
                
                isSuccess = logDal.Delete(intID);
                if (!isSuccess) break;
            }

            if (isSuccess)
            {
                helper.IsSuccess = true;
                helper.Message = "Data has been deleted!";
            }
            else
            {
                helper.IsSuccess = false;
                helper.Message = logDal.GetErrorMessage();

                //sysTLog log = new sysTLog();
                //log.logEvent = "Delete";
                //log.logMessage = logDal.GetErrorMessage();
                //log.logSource = "sys_t_role.ashx";
                //log.createdBy = base.UserLoggedIn;
                //log.createdDate = DateTime.Now;
                //logDal.Save(log);                
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        private string GetData(HttpContext context)
        {
            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            string sidx = ((context.Request.QueryString["sidx"]) == null ? sysTLog.Fields.createdBy : context.Request.QueryString["sidx"].ToString());

            string sord = ((context.Request.QueryString["sord"]) == null ? "DESC" : context.Request.QueryString["sord"].ToString());
            
            string parameters = string.Empty;

            int totalCount = Convert.ToInt32(logDal.SelectScalar(SQL.Function.Aggregate.Count, sysTLog.Fields.logId, parameters));

            decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
            int totalPages = Convert.ToInt32(dc);

            List<sysTLog> pagedList = logDal.GetList(pageSize, page, sidx, sord, parameters);

            if (!string.IsNullOrEmpty(logDal.GetErrorMessage()))
            {
                log = new sysTLog();
                log.logEvent = "GetData Log Error";
                log.logMessage = logDal.GetErrorMessage();
                
            }
            else
            {
                log = new sysTLog();
                log.logEvent = "View Data Log Success";
                log.logMessage = logDal.GetErrorMessage();
            }

            log.logSource = "sys_t_log.ashx";
            log.createdBy = base.UserLoggedIn;
            log.createdDate = DateTime.Now;
            logDal.Save(log);

            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = totalPages,
                records = totalCount,
                rows = pagedList,

            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);
        }

        public string GetDataAllRole(HttpContext context)
        {
            base.ProcessRequest(context);

            List<sysTLog> list = logDal.GetList();
            var result = list;

            return Newtonsoft.Json.JsonConvert.SerializeObject(result);

        }

       

        #endregion

    }
}